package com.wyp.utils.md5;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * 使用两次md5加salt进行加密，防止在网络传播时数据泄密，数据库被盗时数据泄密
 * ((password+salt)+salt)
 *
 * @author: Oliver
 */
public class MD5Utils {
    public static String md5(String src) {
        return DigestUtils.md5Hex(src);
    }

    private static final String salt = "1a2b3c4d";

    /**
     * 转成form提交时密码
     *
     * @param password
     * @return
     */
    public static String inputPassToFormPass(String password) {
        String str = "" + salt.charAt(0) + salt.charAt(2) + password + salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    /**
     * 转成数据库密码
     *
     * @param password
     * @param salt
     * @return
     */
    public static String formPassToDBPass(String password, String salt) {
        String str = "" + salt.charAt(0) + salt.charAt(2) + password + salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    /**
     * 将输入密码转成最终的db密码
     *
     * @param inputPass
     * @param saltDB
     * @return
     */
    public static String inputPassToDbPass(String inputPass, String saltDB) {
        String formPass = inputPassToFormPass(inputPass);
        String dbPass = formPassToDBPass(formPass, saltDB);
        return dbPass;
    }
    public static void main(String[] args) {
        System.out.println(inputPassToFormPass("123456"));//d3b1294a61a07da9b49b6e22b2cbd7f9
    }
}
